package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;

/**
 * Created by chenchangxing on 15/12/24.
 */
public class TestListAdapter extends BaseAdapter {

    Context context;
    boolean isMatch;
    OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        void click(int position);
    }

    public void setOnItemClickListener (OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setSize(int size) {
        this.size = size;
    }

    int size;
    List<Map<Integer, Boolean>> list;
    LayoutInflater inflater;
    Map<Integer, Boolean> map;

    public TestListAdapter(Context context, int size, Map<Integer, Boolean> map, boolean isMatch) {
        this.context = context;
        this.size = size;
        this.inflater = LayoutInflater.from(context);
        this.map = map;
        this.isMatch = isMatch;
    }



    @Override
    public int getCount() {
        return size;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_test_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.titleNum.setText(String.valueOf(position + 1));

        if (map.containsKey(position)) {
            boolean isCorrect = map.get(position);
            holder.titleNum.setTextColor(context.getResources().getColor(R.color.white));
            if (isMatch) {
                holder.titleNum.setBackgroundResource(R.drawable.bg_blue);
            } else {
                if (isCorrect) {
                    holder.titleNum.setBackgroundResource(R.drawable.bg_blue);
                } else {
                    holder.titleNum.setBackgroundResource(R.drawable.bg_test_note_red);
                }
            }
        } else {
            holder.titleNum.setBackgroundResource(R.drawable.bg_white_press);
            holder.titleNum.setTextColor(context.getResources().getColor(R.color.title));
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.click(position);
            }
        });
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_test_list.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.title_num)
        TextView titleNum;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
